<?php
namespace app\adminjcb\controller;

use app\common\controller\AdminBaseController;

/**
 * 管理
 * Class Member
 */
class OrderController extends AdminBaseController {

	protected function _initialize() {
		parent::_initialize();
	}

	/**
	 * 交易列表
	 */
	public function index($keyword = '', $ziduan = 'id') {
		$map = array();
		if ($keyword) {
			$map['b.'.$ziduan] = $keyword;
		}
		$this->assign('ziduan', $ziduan);
		$this->assign("keyword", $keyword);
		if (input("type")) {
			$map['a.type'] = input("type");
		}
		$this->assign("type", input("type"));
		$start_time = input('start_time');
		$end_time = input('end_time');
		if ($start_time && $end_time) {
			$map['a.add_time'] = array(
				'between',
				array(
					$start_time,
					$end_time
				)
			);
		}
		if ($start_time && !$end_time) {
			$map['a.add_time'] = array(
				'>=',
				$start_time
			);
		}
		if (!$start_time && $end_time) {
			$map['a.add_time'] = array(
				'<=',
				$end_time
			);
		}
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
		$field = 'a.*,b.mobile';
		$list = db('order_item')->alias("a")->field($field)->join("member b", "b.id=a.uid")->where($map)->order('a.add_time DESC')->paginate(15, false, get_query());
		$this->assign("list", $list);
		return $this->fetch();
	}

	/**
	 * 删除
	 * @param int   $id
	 * @param array $ids
	 */
	public function delete($id) {
		if ($id) {
			if (db("order_item")->delete($id)) {
				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('请选择需要删除的');
		}
	}

	/**
	 * 委托列表
	 */
	public function wtlist($keyword = '', $ziduan = 'id') {
		$map['a.status'] = 1;
		if ($keyword) {
			$map['b.'.$ziduan] = $keyword;
		}
		$this->assign('ziduan', $ziduan);
		$this->assign("keyword", $keyword);
		if (input("type")) {
			$map['a.type'] = input("type");
		}
		if (input("type")) {
			$map['a.type'] = input("type");
		}
		$this->assign("type", input("type"));
		$start_time = input('start_time');
		$end_time = input('end_time');
		if ($start_time && $end_time) {
			$map['a.add_time'] = array(
				'between',
				array(
					$start_time,
					$end_time
				)
			);
		}
		if ($start_time && !$end_time) {
			$map['a.add_time'] = array(
				'>=',
				$start_time
			);
		}
		if (!$start_time && $end_time) {
			$map['a.add_time'] = array(
				'<=',
				$end_time
			);
		}
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
		$field = 'a.*,b.mobile';
		$list = db('order')->alias("a")->field($field)->join("member b", "b.id=a.uid")->where($map)->order('a.add_time DESC')->paginate(15, false, get_query());
		$this->assign("list", $list);
		return $this->fetch();
	}

	/**
	 * 删除
	 * @param int   $id
	 * @param array $ids
	 */
	public function wt_delete($id) {
		if ($id) {
			if (db("order")->delete($id)) {
				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('请选择需要删除的');
		}
	}

	/**
	 * 矿机列表
	 */
	public function machine($keyword = '', $ziduan = 'id', $mid = '') {
		$map['a.status'] = 1;
		if ($mid) {
			$map['a.mid'] = $mid;
		}
		$this->assign('mid', $mid);
		if ($keyword) {
			$map['b.' . $ziduan] = $keyword;
		}
		$this->assign('ziduan', $ziduan);
		$this->assign("keyword", $keyword);
		$field = 'a.*,b.mobile';
		$list = db('member_machine')->alias("a")->field($field)->join("member b", "b.id=a.uid")->where($map)->order('a.add_time DESC')->paginate(15, false, get_query());
		$this->assign("list", $list);
		$count = db('member_machine')->alias("a")->field($field)->join("member b", "b.id=a.uid")->where($map)->count();
		$this->assign("total_machine", $count);
		$machines = db('machine')->select();
		$this->assign("machines", $machines);
		return $this->fetch();
	}

	/**
	 * 删除
	 * @param int   $id
	 * @param array $ids
	 */
	public function machine_delete($id) {
		if ($id) {
			if (db("member_machine")->delete($id)) {
				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('请选择需要删除的');
		}
	}

	/**
	 * 余额列表
	 */
	public function money($keyword = '', $ziduan = 'id') {
		$map = array();
		if ($keyword) {
			$map['b.' . $ziduan] = $keyword;
		}
		$this->assign('ziduan', $ziduan);
		$this->assign("keyword", $keyword);
		if (input("title")) {
			$map['a.title'] = input("title");
		}
		$this->assign("title", input("title"));
		if (input("note")) {
			$map['a.note'] = array(
				'like',
				'%' . input("note") . '%'
			);
		}
		$this->assign("note", input("note"));
		if (input("type")) {
			$map['a.type'] = input("type");
		}
		$this->assign("type", input("type"));
		if (input("status")) {
			$map['a.status'] = input("status");
		}
		$start_time = input('start_time');
		$end_time = input('end_time');
		if ($start_time && $end_time) {
			$map['a.add_time'] = array(
				'between',
				array(
					$start_time,
					$end_time
				)
			);
		}
		if ($start_time && !$end_time) {
			$map['a.add_time'] = array(
				'>=',
				$start_time
			);
		}
		if (!$start_time && $end_time) {
			$map['a.add_time'] = array(
				'<=',
				$end_time
			);
		}
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
		$field = 'a.*,b.mobile';
		$list = db('money')->alias("a")->field($field)->join("member b", "b.id=a.uid")->where($map)->order('a.add_time DESC')->paginate(15, false, get_query());
		$this->assign("list", $list);
		return $this->fetch();
	}

	/**
	 * 币列表
	 */
	public function coin($keyword = '', $ziduan = 'id') {
		$map = array();
		if ($keyword) {
			$map['b.' . $ziduan] = $keyword;
		}
		$this->assign('ziduan', $ziduan);
		$this->assign("keyword", $keyword);
		if (input("title")) {
			$map['a.title'] = input("title");
		}
		$this->assign("title", input("title"));
		if (input("note")) {
			$map['a.note'] = array(
				'like',
				'%' . input("note") . '%'
			);
		}
		$this->assign("note", input("note"));
		if (input("type")) {
			$map['a.type'] = input("type");
		}
		$this->assign("type", input("type"));
		$start_time = input('start_time');
		$end_time = input('end_time');
		if ($start_time && $end_time) {
			$map['a.add_time'] = array(
				'between',
				array(
					$start_time,
					$end_time
				)
			);
		}
		if ($start_time && !$end_time) {
			$map['a.add_time'] = array(
				'>=',
				$start_time
			);
		}
		if (!$start_time && $end_time) {
			$map['a.add_time'] = array(
				'<=',
				$end_time
			);
		}
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
		$field = 'a.*,b.mobile';
		$list = db('coin')->alias("a")->field($field)->join("member b", "b.id=a.uid")->where($map)->order('a.add_time DESC')->paginate(15, false, get_query());
		$this->assign("list", $list);
		return $this->fetch();
	}

}
